package com.example.walled_houses.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.walled_houses.entity.House;
import com.example.walled_houses.mapper.HouseMapper;
import com.example.walled_houses.service.HouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.walled_houses.utils.UploadCos;
import com.example.walled_houses.vo.Result;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.walled_houses.utils.TengXunConstants.TENGXUN_PATH;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengtaoxie
 * @since 2023-10-24
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    private static final Integer PAGE_SIZE = 5;

    @Resource
    private HouseMapper houseMapper;


    /**
     * 获取围屋信息
     * @param currentPage
     * @return
     */
    @Override
    public Result getHouse(Integer currentPage) {
        Gson gson = new Gson();
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("house_score");
        IPage<House> page = new Page<>(currentPage, PAGE_SIZE);
        IPage<House> housePage = houseMapper.selectPage(page, queryWrapper);
        if(housePage.getTotal() > 0){
            List<House> houseList = housePage.getRecords();
            if(!houseList.isEmpty()){
                houseList.forEach(house -> {
                    String housePicture = house.getHousePicture();
                    if(housePicture != null) {
                        List<String> pictureUrl = gson.fromJson(housePicture, new TypeToken<List<String>>() {
                        }.getType());
                        List<String> photoUrl = new ArrayList<>(pictureUrl.size());
                        pictureUrl.forEach(picture ->{
                            photoUrl.add(TENGXUN_PATH + picture);
                        });
                        house.setPhotoList(photoUrl);
                    }
                });
            }
            return Result.ok(housePage);
        }else {
            return Result.fail().message("未获取到信息");
        }
    }

    /**
     * 上传围屋图片到远程服务器
     * @param files
     * @return
     */
    @Override
    public Result uploadPhoto(MultipartFile[] files) {
        if(files.length == 0){
            return Result.fail().message("上传图片不能为空");
        }
        return UploadCos.uploadPhotos(files);
    }

    /**
     * 增加围屋信息
     * @param house
     * @return
     */
    @Override
    public Result addHouse(House house) {
        List<String> photoList = house.getPhotoList();
        if(photoList != null){
            Gson gson = new Gson();
            String photoUrl = gson.toJson(photoList);
            house.setHousePicture(photoUrl);
        }
        houseMapper.insert(house);
        return Result.ok();
    }



}
